package com.lmc.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lmc.R;
import com.lmc.recreation.RecreationActivity;
import com.lmc.util.GlideUtil;
import com.qxzn.app.bean.Recreation;

import java.util.List;

public class RecreationGridAdapter extends BaseAdapter {
    Context context;
    List<Recreation.DataBean.ItemsBean> list;

    public RecreationGridAdapter(Context context, List<Recreation.DataBean.ItemsBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Recreation.DataBean.ItemsBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.lmc_recreation_grid_item, null);
            holder = new ViewHolder();
            holder.thumbnail = convertView.findViewById(R.id.thumbnail);
            holder.text = convertView.findViewById(R.id.text);
            holder.layout = convertView.findViewById(R.id.layout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Recreation.DataBean.ItemsBean item = getItem(position);

        GlideUtil.setImg(context,holder.thumbnail,item.getImage());
        holder.text.setText(item.getName());


        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RecreationActivity.class);
                intent.putExtra("id",item.getId());
                context.startActivity(intent);
            }
        });


        return convertView;
    }

    private class ViewHolder {
        ImageView thumbnail;
        TextView text;
        View layout;
    }
}
